package com.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.entity.District;
import com.hy.mapper.DistrictMapper;
import com.hy.service.DistrictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DistrictServiceImpl extends ServiceImpl<DistrictMapper, District> implements DistrictService {

    @Override
    public List<District> getAllDistricts() {
        LambdaQueryWrapper<District> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(District::getDeleteFlag, 0)
               .orderByAsc(District::getDistrictsName);
        return list(wrapper);
    }

    @Override
    public District getDistrictById(Long id) {
        LambdaQueryWrapper<District> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(District::getId, id)
               .eq(District::getDeleteFlag, 0);
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public boolean addDistrict(District district) {
        district.setDeleteFlag(0);
        district.setCreateTime(LocalDateTime.now());
        district.setUpdateTime(LocalDateTime.now());
        return save(district);
    }

    @Override
    @Transactional
    public boolean updateDistrict(District district) {
        district.setUpdateTime(LocalDateTime.now());
        return updateById(district);
    }

    @Override
    @Transactional
    public boolean deleteDistrict(Long id) {
        LambdaUpdateWrapper<District> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(District::getId, id)
                    .set(District::getDeleteFlag, 1)
                    .set(District::getUpdateTime, LocalDateTime.now());
        return update(updateWrapper);
    }
} 